<?php
namespace Org\Util;
use Think\Exception;

Vendor('getui.IGt','','.Push.php');
class GeTui {
    private $host = 'http://sdk.open.api.igexin.com/apiex.htm';

    private $appkey = 'JPePoBjiCkACxE1NhvDoh1';
    private $appid = 'vM6wj0G9CUAciFQuh3pVa8';
    private $mastersecret = '1Bc5HfJcsU8nka6evgWcz5';

    //单推接口案例
    function pushmsg($content1,$content,$cid){
        $igt = new \IGeTui($this->host,$this->appkey,$this->mastersecret);

        //消息模版：
        $template = $this->IGtTransmissionTemplate($content1,$content);
        $message = new \IGtSingleMessage();

        $message->set_isOffline(true);//是否离线
        $message->set_offlineExpireTime(3600*12*1000);//离线时间
        $message->set_data($template);//设置推送消息类型
        $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，2为4G/3G/2G，1为wifi推送，0为不限制推送
        //接收方
        $target = new \IGtTarget();
        $target->set_appId($this->appid);
        $target->set_clientId($cid);
        try {
            $rep = $igt->pushMessageToSingle($message, $target);
            return $rep;
        }catch(\RequestException $e){
            $requstId =e.getRequestId();
            //失败时重发
            $rep = $igt->pushMessageToSingle($message, $target,$requstId);
            return $rep;
        }
    }
    /**
     * 透传消息模板
     * @return \IGtTransmissionTemplate
     * @throws \Exception
     */
    function IGtTransmissionTemplate($content1,$content){
        $template =  new \IGtTransmissionTemplate();
        $template->set_appId($this->appid);//应用appid
        $template->set_appkey($this->appkey);//应用appkey
        $template->set_transmissionType(1);//透传消息类型
        $template->set_transmissionContent($content);//透传内容
        //APN高级推送
        $apn = new \IGtAPNPayload();
        $alertmsg=new \DictionaryAlertMsg();
        $alertmsg->title="洋玩易";
        $alertmsg->body=$content1;
        $alertmsg->actionLocKey="ActionLockey";
        $alertmsg->locKey="LocKey";
        $alertmsg->locArgs=array("locargs");
        $alertmsg->launchImage="launchimage";
//        IOS8.2 支持
        $alertmsg->titleLocKey="TitleLocKey";
        $alertmsg->titleLocArgs=array("TitleLocArg");

        $apn->alertMsg=$alertmsg;
        //$apn->badge=7;
        $apn->sound="";
        $apn->add_customMsg("payload","payload");
        //$apn->contentAvailable=1;
        $apn->category="ACTIONABLE";
        $template->set_apnInfo($apn);
        return $template;
    }
}